import store from "@/store";
import {logOut} from "@/util/Api";
import {actions as userActions} from "@/store/common/user";
import {errorMsg, queryState} from "@/util/global";
import EventBus from "@/util/EventBus";

/**
 * 重新加载用户信息
 */
export function resetUserInfo() {
    // /* 实时更新 */
    // regainUserInfo()
    //     .then(response => {
    //         if(response.data){
    //             let action = {
    //                 type: INIT_USER_INFO,
    //                 value: response.data
    //             };
    //             store.dispatch(action);
    //             sessionStorage.setItem('just-game-obj',toBase64(response.data));
    //         }
    // })
    EventBus.emit('RegainUserInfo', '');
}

/**
 * 清空用户信息
 */
export function clearUserInfo() {
    logOut().then(
        res => {
            store.dispatch(userActions.clearBoth());
            sessionStorage.removeItem("just-game-obj");
            sessionStorage.removeItem("token");
        }
    ).catch(
        error => errorMsg("服务器异常！")
    );
}

/**
 * 查询用户的vip类型
 * 0未开通vip
 * 1普通vip
 * 2年费vip
 * 2永久vip
 */
export function userVipType() {
    return queryState('user.vipType') || 0;
    // let vipExpireDate = queryState('user.vipExpireDate');
    // let vipEffectDate = queryState('user.vipEffectDate');
    // if (!vipExpireDate || !vipEffectDate){
    //     return 0;
    // }
    // let remainingDays = moment(vipExpireDate).diff(new moment(vipEffectDate),'day');
    // console.log("remainingDays",remainingDays);
    // if (remainingDays === 9999){
    //     return 3;
    // }else if (remainingDays === 365){
    //     return 2;
    // }else{
    //     return 1;
    // }
}
